Ext.define('console.login', {
    extend: 'Ext.ux.desktop.App',

    requires: [
        'Ext.window.MessageBox',
        'Ext.ux.desktop.ShortcutModel',
        'console.Settings',
        'console.gradeScoreWindow',
        'console.testPaperWindow',
        'console.internalEvalMapperWindow',
        'console.questionWindow',
        'console.deptGroupWindow',
        'console.deptWindow',
        'console.evalManageWindow',
        'console.evalPageWindow'
    ],

    init: function () {
        this.callParent();
        this.checkLogin();
    },

    getModules: function () {
        return [
            new console.gradeScoreWindow(),
            new console.testPaperWindow(),
            new console.internalEvalMapperWindow(),
            new console.questionWindow(),
            new console.deptGroupWindow(),
            new console.deptWindow(),
            new console.evalManageWindow(),
            new console.evalPageWindow()
        ];
    },

    getDesktopConfig: function () {
        var me = this, ret = me.callParent();
        return Ext.apply(ret, {
            //cls: 'ux-desktop-black',
            contextMenuItems: [
                {text: '设置背景', handler: me.onSettings, scope: me}
            ],
            shortcuts: Ext.create('Ext.data.Store', {
                model: 'Ext.ux.desktop.ShortcutModel',
                data: [

                ]
            }),

            wallpaper: 'console/wallpapers/Blue-Sencha.jpg',
            wallpaperStretch: false
        });
    },

    // config for the start menu
    getStartConfig: function () {
        var me = this, ret = me.callParent();

        return Ext.apply(ret, {
            title: '用户',
            iconCls: 'user',
            height: 300,
            toolConfig: {
                width: 100,
                items: [
                    {
                        text: 'Settings',
                        iconCls: 'settings',
                        handler: me.onSettings,
                        scope: me
                    },
                    '-',
                    {
                        text: 'Logout',
                        iconCls: 'logout',
                        handler: me.onLogout,
                        scope: me
                    }
                ]
            }
        });
    },

    getTaskbarConfig: function () {
        var ret = this.callParent();

        return Ext.apply(ret, {
            quickStart: [
                // {name: 'Accordion Window', iconCls: 'accordion', module: 'acc-win'},
                // {name: 'Grid Window', iconCls: 'icon-grid', module: 'grid-win'}
            ],
            trayItems: [
                {xtype: 'trayclock', flex: 1}
            ]
        });
    },

    checkLogin: function () {
        Ext.create('Ext.window.Window', {
            modal: true,
            title: '请登录',
            closable: false,
            items: [{
                xtype: 'form',
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield'
                },
                items: [{
                    name: 'username',
                    fieldLabel: '用户名',
                    allowBlank: false
                }, {
                    name: 'password',
                    fieldLabel: '密码',
                    inputType: 'password',
                    allowBlank: false
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'user/normalLogin',
                                success: function (form, action) {
                                    var res = Ext.decode(action.response.responseText);
                                    if  (res.type=='A'){
                                        //Ext.Msg.alert('提示', res.type);
                                        window.location.href="/eval/internalEval.jsp";
                                    }else{
                                        //Ext.Msg.alert('提示', res.type);
                                        window.location.href="/eval/internalEvalB.jsp";
                                    }

                                },
                                failure: function (form, action) {
                                    var res = Ext.decode(action.response.responseText);
                                    Ext.Msg.alert('提示', res.failure);
                                }
                            });
                        }
                    }
                }]
            }]
        }).show();
    },

    onLogout: function () {
        Ext.Msg.confirm('注销', '您确定注销登录吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'user/logout',
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('提示', '注销成功');
                            window.location.reload();
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        });
    },

    onSettings: function () {
        var dlg = new console.Settings({
            desktop: this.desktop
        });
        dlg.show();
    }
});